package com.monboo.album.view.component
{
	import com.gs.TweenLite;
	import com.monboo.album.command.AlbumEvent;
	import com.monboo.album.common.AlbumAction;
	import com.monboo.album.common.AlbumConst;
	import com.monboo.utils.FullState;
	import com.monboo.utils.GetStage;
	import com.monboo.album.view.AlbumInfoMediator;
	import com.yahoo.astra.utils.DynamicRegistration;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	public class Picture extends AbstractChangeUI
	{
		private var loadImage:LoadImage;
		private var picArr:Array = new Array();
		
		private var fadeLayer:Sprite = new Sprite;
		private var showLayer:Sprite = new Sprite;
		private var fadeState:Boolean = false;
		
		private var imageInfo:AlbumInfoMediator;
		private var cPoint:Point;
		private var quickShow:Boolean;
		
		public function Picture(_picArr:Array,_imageInfo:AlbumInfoMediator)
		{
			super(_imageInfo);
			picArr = _picArr;
			imageInfo = _imageInfo;
			
			loadImage = new LoadImage();
			loadImage.addEventListener(AlbumEvent.LOAD_IMAGE_COMPLETE,loadImageComplete);
			
			toWidth = AlbumConst.imageWidth;
			toHeight = AlbumConst.imageHeight;
			addChild(showLayer);
			addChild(fadeLayer);
		}
		public function toChangeSize():void{
			changeSize(showLayer.getChildByName("picContent") as Sprite);
			if(fadeState){
				changeSize(fadeLayer.getChildByName("picContent") as Sprite);
			}
		}
		public function startShow():void{
			var lcontex:LoaderContext = new LoaderContext;
			lcontex.checkPolicyFile = true;
			var ld:Loader = new Loader();
			ld.contentLoaderInfo.addEventListener(Event.COMPLETE,startLoaded);
			ld.load(new URLRequest(picArr[0]),lcontex);
		}
		private function startLoaded(e:Event):void{
			var picContent:Sprite = new Sprite();
			picContent.name = "picContent";
			picContent.addChild(new Bitmap(e.target.loader.content["bitmapData"],"auto",true))
			showLayer.addChild(picContent);
			
			if(imageInfo.scaleIn){
				doScale();
			}else{
				changeSize(showLayer.getChildByName("picContent") as Sprite);
			}
			
			dispatchEvent(new Event(AlbumEvent.PLAY_TRANSITION));
			
			doAutoNext();
		}
		
		public function show(n:int,q:String=null):void{
			if(q == "slow"){
				quickShow = false;
			}else{
				quickShow = true;
			}
			loadImage.load(picArr[n]);
		}
		
		private function loadImageComplete(e:Event):void{
			var scaleX:Number = showLayer.scaleX;
			var scaleY:Number = showLayer.scaleY;
			var fadeContent:Sprite = showLayer.getChildByName("picContent") as Sprite;
			fadeLayer.addChild(fadeContent);
			
			changeSize(fadeContent,0);
			cPoint = new Point(fadeContent.width/2,fadeContent.height/2);	
			DynamicRegistration.scale(fadeLayer,cPoint,scaleX,scaleY);
			
			while(showLayer.numChildren){showLayer.removeChildAt(0);}
			var picContent:Sprite = new Sprite();
			picContent.name = "picContent";
			picContent.addChild(loadImage.data)
			showLayer.addChild(picContent);
			showLayer.visible = false;
			
			fadeState = true;
			
			TweenLite.killTweensOf(fadeLayer);
			TweenLite.killDelayedCallsTo(delayAutoNext);
			
			if(imageInfo.autoState && !quickShow){
				TweenLite.to(fadeLayer,.8,{alpha:0,onComplete:onFinishTween});
			}else{	
				quickFinishTween();
			}
		}
		
		
		private function quickFinishTween():void{	
			if(fadeState){
				fadeState = false;
				while(fadeLayer.numChildren){fadeLayer.removeChildAt(0);}
				fadeLayer.alpha = 1;
				showLayer.visible = true;
				
				if(imageInfo.scaleIn){
					doScale();
				}else{
					changeSize(showLayer.getChildByName("picContent") as Sprite);
				}
				
				//dispatchEvent(new Event(AlbumEvent.PLAY_TRANSITION));
				//点缩略图切换图片关闭转场功能，能提高切换速度
				
				if(!imageInfo.gotoStop){
					doAutoNext();
				}
			}
		}
		private function onFinishTween():void{	
			fadeState = false;
			while(fadeLayer.numChildren){fadeLayer.removeChildAt(0);}
			fadeLayer.alpha = 1;
			showLayer.visible = true;
			
			if(imageInfo.scaleIn){
				doScale();
			}else{
				changeSize(showLayer.getChildByName("picContent") as Sprite);
			}
			
			dispatchEvent(new Event(AlbumEvent.PLAY_TRANSITION));
			
			doAutoNext();
		}
		
		private function doAutoNext():void{
			TweenLite.delayedCall(imageInfo.autoTime,delayAutoNext);
		}
		private function delayAutoNext():void{
			if(imageInfo.autoState){
				dispatchEvent(new Event(AlbumEvent.AUTO_PLAY));
			}
		}
		
		private function doScale():void{
			var picContent:Sprite = showLayer.getChildByName("picContent") as Sprite;	
			changeSize(picContent,0);
			
			cPoint = new Point(picContent.width/2,picContent.height/2);	
			/*****
			 ** 切换图片放大进入效果 
			 ** 
			 ********/
			endScaleFunc();
			TweenLite.killDelayedCallsTo(endScaleFunc);
			
			addEventListener(Event.ENTER_FRAME,scaleEnterFrameFunc);
			TweenLite.delayedCall(4,endScaleFunc);
			
		}
		private function endScaleFunc():void{
			removeEventListener(Event.ENTER_FRAME,scaleEnterFrameFunc);
		}
		private function scaleEnterFrameFunc(e:Event):void{
			DynamicRegistration.scale(showLayer,cPoint,showLayer.scaleX+0.0036,showLayer.scaleY+0.0036);
		}
		
		override public function changeSize(pic:Sprite,degrees:Number=0):void {
			w = pic.parent.width;
			h = pic.parent.height;
			if(FullState.fullState){
				toWidth = GetStage.theStage().stageWidth;
				toHeight= GetStage.theStage().stageHeight;
				
				setSize(pic,toWidth,toHeight,w,h);
				setPosition(pic,toWidth,toHeight,degrees);
			}else{
				toWidth = imageInfo.imageWidth;
				toHeight = imageInfo.imageHeight;
				
				setSize(pic,toWidth,toHeight,w,h);
				setPosition(pic,toWidth, toHeight,degrees);
			}		
		}
		override protected function setSize(pic:Sprite,bigPicWidth:Number,bigPicHeight:Number,w:Number,h:Number):void{
			pic.parent.scaleX = pic.parent.scaleY = 1;
			if(w/h > bigPicWidth/bigPicHeight){
				pic.parent.scaleX = pic.parent.scaleY = bigPicWidth/pic.parent.width;
			}else if(w/h < bigPicWidth/bigPicHeight){
				pic.parent.scaleX = pic.parent.scaleY = bigPicHeight/pic.parent.height;
			}else if(w/h == bigPicWidth/bigPicHeight){
				pic.parent.scaleX = pic.parent.scaleY = bigPicWidth/w;
			}
		}	
		override protected function setPosition(pic:Sprite,w:Number,h:Number,degrees:Number=0):void{
			if(FullState.fullState){
				top=0;
				left=0;
			}else{
				top=imageInfo.imageY;		
				left=imageInfo.imageX;
			}
			
			if(degrees == 90){
				pic.parent.y = (h - pic.parent.height)/2 + top;
				pic.parent.x = (w + pic.parent.width)/2 + left;
			}else if(degrees == 180){
				pic.parent.y = (h - pic.parent.height)/2 + pic.parent.height + top;
				pic.parent.x = (w + pic.parent.width)/2 + left;
			}else if(degrees == 270){
				pic.parent.y = (h - pic.parent.height)/2 + pic.parent.height + top;
				pic.parent.x = (w - pic.parent.width)/2 + left;
			}else{
				pic.parent.y = (h - pic.parent.height)/2 + top;
				pic.parent.x = (w - pic.parent.width)/2 + left;
			}
		}
	}
}